var port = (process.env.VMC_APP_PORT || 8080);
var host = (process.env.VCAP_APP_HOST || 'localhost');

var http = require('http'),
    router = require('router'),
    Q = require('q'),
    shopService = require('./services/shopservice'),
    alertService = require('./services/alertservice'),
    //alertCreationService = require('./services/alertcreationservice'),
    imageService = require('./services/imageservice'),
    userService = require('./services/userservice'),
    userCreationService = require('./services/usercreationservice'),
    shopCreationService = require('./services/shopcreationservice'),
    shopCategoryService = require('./services/shopcategoryservice'),
    searchService = require('./services/searchservice'),
    researchService = require('./services/researchservice'),
    alertReportService = require('./services/alertreportservice'),
    shopReportService = require('./services/shopreportservice'),
    userReportService = require('./services/userreportservice'),
    tagService = require('./services/tagservice'),
    referenceService = require('./services/referenceservice'),
    locationCityService = require('./services/locationcityservice'),
    appRouteService = require('./services/app/routeservice'),
    proRouteService = require('./services/pro/routeservice'),
    adminRouteService = require('./services/admin/routeservice'),
    guessRouteService = require('./services/guess/routeservice'),
    emailService = require('./services/emailservice'),
    scheduleService = require('./services/scheduleservice'),
    exceptionService = require('./services/exceptionservice');

router.init({
    debug: true,
    secret: 'code secret 007',
    bundles: {

        '/app.js': ['web/libs/hammer.js',
                    'web/libs/angular.js',
                    'web/libs/angular-material.js',
                    'web/libs/angular-route.js',
                    'web/libs/angular-aria.js',
                    'web/libs/angular-sanitize.js',
                    'web/libs/angular-locale_fr-fr.js',
                    'web/libs/angular-animate.js',
                    'web/libs/ui-bootstrap-light-tpls-0.12.0.js',
                    'web/app/controllers/app.js',
                    'web/app/services/queryparser.js',
                    'web/app/services/localstorage.js',
                    'web/app/services/image.js',
                    'web/app/services/geolocation.js',
                    'web/app/services/token.js',
                    'web/app/services/indexeddb.js',
                    'web/app/services/store.js',
                    'web/app/services/hashlist.js',
                    'web/app/services/dataproviders.js',
                    'web/app/services/datamodels.js',
                    'web/app/services/canceler.js',
                    'web/app/services/connect.js',
                    'web/app/services/googlegeocode.js',
                    'web/app/services/toast/confirm.js',
                    'web/app/services/toast/exception.js',
                    'web/app/services/toast/notify.js',
                    'web/app/services/crop.js',
                    'web/app/services/filereader.js',
                    'web/pro/services/connectPro.js',
                    'web/app/directives/infiniteScroll.js', 
                    'web/app/directives/ngsrccache.js',
                    'web/app/directives/crop.js',
                    'web/app/filters/linkify.js',
                    'web/app/filters/subheader.js',
                    'web/app/filters/slice.js',
                    'web/app/filters/mediaScreen.js'],

        '/app.css': ['web/libs/angular-material.css',
                     'web/assets/md.scss',
                     'web/assets/ui-boostrap.css',
                     'web/assets/master.scss',
                     'web/assets/animations.css',
                     'web/assets/components/page.scss',
                     'web/assets/components/toolbar.css',
                     'web/assets/components/menu.scss',
                     'web/assets/components/login.scss',
                     'web/assets/components/alerts.scss',
                     'web/assets/components/account.css',
                     'web/assets/components/subscriptions.css',
                     'web/assets/components/shop.scss',
                     'web/assets/components/alert.scss',
                     'web/assets/components/shops.css',
                     'web/assets/components/users.css',
                     'web/assets/components/visit.scss'],

        '/pro.js': ['web/libs/hammer.js',
                    'web/libs/angular.js',
                    'web/libs/angular-material.js',
                    'web/libs/angular-route.js',
                    'web/libs/angular-aria.js',
                    'web/libs/angular-sanitize.js',
                    'web/libs/angular-locale_fr-fr.js',
                    'web/libs/angular-animate.js',
                    'web/libs/ui-bootstrap-light-tpls-0.12.0.js',
                    'web/pro/controllers/pro.js',
                    'web/pro/controllers/common.js',
                    'web/app/services/queryparser.js',
                    'web/app/services/localstorage.js',
                    'web/app/services/image.js',
                    'web/app/services/geolocation.js',
                    'web/app/services/token.js',
                    'web/app/services/indexeddb.js',
                    'web/app/services/store.js',
                    'web/app/services/hashlist.js',
                    'web/app/services/dataproviders.js',
                    'web/app/services/datamodels.js',
                    'web/app/services/canceler.js',
                    'web/app/services/googlegeocode.js',
                    'web/app/services/toast/confirm.js',
                    'web/app/services/toast/exception.js',
                    'web/app/services/toast/notify.js',
                    'web/app/services/crop.js',
                    'web/app/services/filereader.js',
                    'web/pro/services/connectPro.js',
                    'web/pro/services/storePro.js',
                    'web/pro/services/dataprovidersPro.js',
                    'web/pro/services/datamodelsPro.js',
                    'web/app/directives/infiniteScroll.js', 
                    'web/app/directives/ngsrccache.js',
                    'web/app/directives/crop.js',
                    'web/app/filters/linkify.js',
                    'web/app/filters/subheader.js',
                    'web/app/filters/slice.js',
                    'web/app/filters/mediaScreen.js'],

        '/pro.css': ['web/libs/angular-material.css',
                     'web/assets/md.scss',
                     'web/assets/ui-boostrap.css',
                     'web/assets/master.scss',
                     'web/assets/animations.css',
                     'web/assets/components/page.scss',
                     'web/assets/components/toolbar.css',
                     'web/assets/components/menu.scss',
                     'web/assets/components/login.scss',
                     'web/assets/components/alerts.scss',
                     'web/assets/components/account.css',
                     'web/assets/components/subscriptions.css',
                     'web/assets/components/shop.scss',
                     'web/assets/components/alert.scss',
                     'web/assets/components/shops.css',
                     'web/assets/components/users.css',
                     'web/assets/components/editor.scss'],

        '/admin.js': ['web/libs/hammer.js',
                    'web/libs/angular.js',
                    'web/libs/angular-material.js',
                    'web/libs/angular-route.js',
                    'web/libs/angular-aria.js',
                    'web/libs/angular-sanitize.js',
                    'web/libs/angular-locale_fr-fr.js',
                    'web/libs/angular-animate.js',
                    'web/libs/ui-bootstrap-light-tpls-0.12.0.js',
                    'web/admin/controllers/admin.js',
                    'web/pro/controllers/common.js',
                    'web/app/services/queryparser.js',
                    'web/app/services/localstorage.js',
                    'web/app/services/image.js',
                    'web/app/services/geolocation.js',
                    'web/app/services/token.js',
                    'web/app/services/indexeddb.js',
                    'web/app/services/store.js',
                    'web/app/services/hashlist.js',
                    'web/app/services/dataproviders.js',
                    'web/app/services/datamodels.js',
                    'web/app/services/canceler.js',
                    'web/admin/services/connectAdmin.js',
                    'web/app/services/googlegeocode.js',
                    'web/app/services/toast/confirm.js',
                    'web/app/services/toast/exception.js',
                    'web/app/services/toast/notify.js',
                    'web/app/services/crop.js',
                    'web/app/services/filereader.js',
                    'web/admin/services/dataprovidersAdmin.js',
                    'web/admin/services/datamodelsAdmin.js',
                    'web/app/directives/infiniteScroll.js', 
                    'web/app/directives/ngsrccache.js',
                    'web/app/directives/crop.js',
                    'web/app/filters/linkify.js',
                    'web/app/filters/subheader.js',
                    'web/app/filters/slice.js',
                    'web/app/filters/mediaScreen.js'],

        '/admin.css': ['web/libs/angular-material.css',
                     'web/assets/md.scss',
                     'web/assets/ui-boostrap.css',
                     'web/assets/master.scss',
                     'web/assets/animations.css',
                     'web/assets/components/page.scss',
                     'web/assets/components/toolbar.css',
                     'web/assets/components/menu.scss',
                     'web/assets/components/login.scss',
                     'web/assets/components/alerts.scss',
                     'web/assets/components/account.css',
                     'web/assets/components/subscriptions.css',
                     'web/assets/components/shop.scss',
                     'web/assets/components/alert.scss',
                     'web/assets/components/shops.css',
                     'web/assets/components/users.css',
                     'web/assets/components/editor.scss']
    }
}).then(function () {
    console.log("register routes");
    //objectName instanceof Function //diff between prototype and instance

    //router.get('/api/shop/near/:shopId').register(shopService, "getNearestShopById");
    
    router.get('/api/shop').register(shopService, "getFromToken");
    router.get('/api/shop/subscriber/users').register(shopService, "getUserSubscribersFromToken");
    router.get('/api/shop/subscriber/shops').register(shopService, "getShopSubscribersFromToken");
    router.get('/api/shop/subscriptions').register(shopService, "getShopSubscriptionsFromToken");
    router.get('/api/shop/full/:shopId').register(shopService, "getFullById");
    router.get('/api/shop/full').register(shopService, "getFullFromToken");
    router.get('/api/shop/:login').register(shopService, "getByLogin");

    router.post('/api/shop/subscribe/user').register(shopService, "addUserSubscriberFromToken");
    router.post('/api/shop/unsubscribe/user').register(shopService, "removeUserSubscriberFromToken");
    router.post('/api/shop/subscribe/shop').register(shopService, "addShopSubscriberFromToken");
    router.post('/api/shop/unsubscribe/shop').register(shopService, "removeShopSubscriberFromToken");
    router.post('/api/shop/save').register(shopService, "save");
    router.post('/api/shop/delete').register(shopService, "remove");

    router.get('/api/shopcreation/empty').register(shopCreationService, "empty");

    router.get('/api/alert/gps').register(alertService, "getByGps");
    router.get('/api/alert/shop/employee').register(alertService, "getByShopEmployeeFromToken");
    router.get('/api/alert/shop/:login').register(alertService, "getByShopLogin");
    router.get('/api/alert/shop').register(alertService, "getByShopFromToken");
    router.get('/api/alert/near/shop').register(alertService, "getByShopNearFromToken");
    router.get('/api/alert/subscriber/shop').register(alertService, "getByShopSubscriberFromToken");
    router.get('/api/alert/like/shop').register(alertService, "getByShopLikeFromToken");
    router.get('/api/alert/near/user').register(alertService, "getByUserNearFromToken");
    router.get('/api/alert/subscriber/user').register(alertService, "getByUserSubscriberFromToken");
    router.get('/api/alert/subscriber/user/:userId').register(alertService, "getByUserSubscriberFromUserId");
    router.get('/api/alert/like/user').register(alertService, "getByUserLikeFromToken");
    router.get('/api/alert/archive/:alertId').register(alertService, "getArchivedById");
    router.get('/api/alert/:alertId').register(alertService, "getById");

    router.post('/api/alert/save').register(alertService, "save");
    router.post('/api/alert/delete').register(alertService, "remove");
    router.post('/api/alert/user/delete').register(alertService, "removeUser");
    router.post('/api/alert/read').register(alertService, "setAlertAsRead");
    router.post('/api/alert/like').register(alertService, "setAlertAsLike");
    router.post('/api/alert/like/delete').register(alertService, "unsetAlertAsLike"); 
    
    //router.get('/api/alertcreation/empty').register(alertCreationService, "empty");

    router.get('/api/users').register(userService, "getListStartsWithLogin");
    router.get('/api/user/subscriptions').register(userService, "getSubscriptionsFromToken");
    router.get('/api/user/full/:userId').register(userService, "getFullById");
    router.get('/api/user/full').register(userService, "getFullFromToken");
    router.get('/api/user/login/:login').register(userService, "getByLogin");
    router.get('/api/user/:userId').register(userService, "getById");
    router.get('/api/user').register(userService, "getFromToken");

    router.post('/api/user/save').register(userService, "save");
    router.post('/api/user/delete').register(userService, "remove");
    router.post('/api/user/password/recovery').register(userService, "passwordRecovery");

    router.get('/api/usercreation/validate/:token').register(userCreationService, "validate");
    //router.get('/api/usercreation/empty').register(userCreationService, "empty");
    router.post('/api/usercreation/create').register(userCreationService, "create");

    router.get('/api/image/shop/front/:id/:media').register(imageService, "getShopFront");
    router.get('/api/image/shop/front/:id').register(imageService, "getShopFront");
    router.get('/api/image/shop/picto/:id/:media').register(imageService, "getShopPicto");
    router.get('/api/image/shop/picto/:id').register(imageService, "getShopPicto");
    router.get('/api/image/user/picto/:id/:media').register(imageService, "getUserPicto");
    router.get('/api/image/user/picto/:id').register(imageService, "getUserPicto");
    router.get('/api/image/alert/image/:id/:media').register(imageService, "getAlertImage");

    router.post('/api/image/shop/front').register(imageService, "saveShopFront");
    router.post('/api/image/shop/picto').register(imageService, "saveShopPicto");
    router.post('/api/image/user/picto').register(imageService, "saveUserPicto");
    router.post('/api/image/alert/image').register(imageService, "saveAlertImage");

    router.get('/api/search').register(searchService, "search");
    router.get('/api/search/shop').register(searchService, "searchShop");
    router.get('/api/search/user').register(searchService, "searchUser");
    router.get('/api/search/alert').register(searchService, "searchAlert");
    router.get('/api/archives/alert').register(searchService, "searchArchivedAlert");

    router.get('/api/research').register(researchService, "getStartsWith");
    router.post('/api/research/call').register(researchService, "call");

    router.get('/api/references/search').register(referenceService, "search");
    //router.get('/api/page/:login').register(referenceService, "getPageByLogin");

    router.get('/api/reports/alert/shop').register(alertReportService, "getFromShopToken");
    router.get('/api/reports/shop').register(shopReportService, "getFromToken");
    router.get('/api/reports/user').register(userReportService, "getFromToken");

    router.get('/api/shopcategory').register(shopCategoryService, "getGroupList");

    router.get('/api/tag/search').register(tagService, "search");
    router.get('/api/hashtag/search').register(tagService, "searchHashtag");
    
    router.get('/api/location/search/city').register(locationCityService, "search");
    router.get('/api/location/city/:city').register(locationCityService, "getByName");
    router.get('/api/location/city').register(locationCityService, "getByName");
    router.get('/api/location/cities').register(locationCityService, "list");

    router.get('/').register(appRouteService, "index");
    router.post('/connectfrom').register(appRouteService, "connectFromToken");
    router.post('/connect').register(appRouteService, "connect");

    router.get('/pro').register(proRouteService, "index");
    router.post('/pro/connectfrom').register(proRouteService, "connectFromToken");
    router.post('/pro/connect').register(proRouteService, "connect");

    router.get('/admin').register(adminRouteService, "index");
    router.post('/admin/connect').register(adminRouteService, "connect");

    router.get('/guess').register(guessRouteService, "index");

}).then(function () {
    return start();
}).fail(function (e) {
    console.log("Error", e.message, e.stack);
});

var start = function () {
    return Q.fcall(function () {
        var server = http.createServer(function (request, response) {
            router.invoke(request, response).fail(function (error) {
                return exceptionService.send(request, response, error);
            }).done();
        });
        server.listen(port, host);
        console.log("server started on " + host + ":" + port + ".");
        return server;
    }).then(function () {
        return scheduleService.start();
    }).fail(function (error) {
        return start();
    });
};

